package com.game;

import cn.hutool.json.JSONObject;

import javax.websocket.*;
import java.net.URI;
import java.util.Map;

@ClientEndpoint
public class MyWebSocketClient {

    @OnOpen
    public void onOpen(Session session) {
        System.out.println("Session opened");
    }

    @OnMessage
    public void onMessage(String message) {
        System.out.println("Received message: " + message);
    }

    @OnClose
    public void OnClose(CloseReason closeReason) {
        System.out.println("Session close: " + closeReason);
    }
    @OnError
    public void OnError(Throwable throwable) {
        System.out.println("Session error: " + throwable);
    }

    public static void main(String[] args) {
        try {
            WebSocketContainer container = ContainerProvider.getWebSocketContainer();
            String uri = "ws://localhost:8888/game/websocket?token=13688888888"; // 替换为你的WebSocket服务器地址
            Session session = container.connectToServer(MyWebSocketClient.class, URI.create(uri));
            JSONObject jsonObject = new JSONObject(session);
            System.out.println(jsonObject);
            for(int i=0;i<1;i++){
                session.getAsyncRemote().sendText("Hello Server!"+i);
                Thread.sleep(1000L);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
